﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;

namespace Test
{
    class Process
    {
        public List<string> ReadText(string filename)
        {
            List<string> lst = new List<string>();
            StreamReader f = new StreamReader(filename);
            string line = f.ReadLine();
            while(line!=null)
            {
                lst.Add(line);
                line = f.ReadLine();
            }
            return lst;
        }
        public int GetMax(int [] input)
        {
            int max=0;
            foreach(int i in input)
            {
                if (max > i) continue;
                else max=i;
            }
            return max;
        }
        public string [,] ConvertListToArr(List<string> list)
        {
            List<int> MNV = new List<int>();
            int col = 0;
            for(int i=0;i<list.Count;i++)
            {
                if (list[i].IndexOf('#')==0) MNV.Add(i);
            }
           
           
            int[] column = new int[MNV.Count - 1];
            for (int i = 0; i < MNV.Count - 1;i++ )
            {
                column[i] = MNV[i + 1] - MNV[i];
            }

            int max = GetMax(column);
            if (list.Count - MNV[MNV.Count - 1]  > max) col = list.Count - MNV[MNV.Count - 1] ;
            else col = max;
            string[,] obj = new string[MNV.Count, col];
            for (int i = 0; i < MNV.Count; i++)
            {
                obj[i, 0] = list[MNV[i]];
            }
                for (int i = 1; i < col; i++)
                {
                    if (i < list.Count - MNV[MNV.Count - 1] ) obj[MNV.Count - 1, i] = list[MNV[MNV.Count - 1] + i];
                }
            for(int i=0;i<MNV.Count-1;i++)
                for(int j=1;j<col;j++)
                {
                    if (j < MNV[i + 1] - MNV[i]) obj[i, j] = list[MNV[i] + j];
                }
            return obj;
        }
        public DataTable ExportArrToTable(string[,] input)
        {
            DataTable dt = new DataTable();
            DataColumn clm1 = new DataColumn();
            clm1.DataType = Type.GetType("System.String");
            clm1.ColumnName = "Mã khách hàng";
            dt.Columns.Add(clm1);
           /*DataColumn clm2 = new DataColumn();
            clm2.DataType = Type.GetType("System.String");
            clm2.ColumnName = "thông tin 1";
            dt.Columns.Add(clm2);
            DataColumn clm3 = new DataColumn();
            clm3.DataType = Type.GetType("System.String");
            clm3.ColumnName = "thông tin 2";
            dt.Columns.Add(clm3);
            DataColumn clm4 = new DataColumn();
            clm4.DataType = Type.GetType("System.String");
            clm4.ColumnName = "thông tin 3";
            dt.Columns.Add(clm4);
            DataColumn clm5 = new DataColumn();
            clm5.DataType = Type.GetType("System.String");
            clm5.ColumnName = "thông tin 4";
            dt.Columns.Add(clm5);
            for(int i=0;i<input.GetLength(0);i++)
            {
                DataRow row = dt.NewRow();
                row["Mã khách hàng"] = input[i, 0];
                row["thông tin 1"] = input[i, 1];
                row["thông tin 2"] = input[i, 2];
                row["thông tin 3"] = input[i, 3];
                row["thông tin 4"] = input[i, 4];
                dt.Rows.Add(row);
            }
            */
            DataColumn[] column = new DataColumn[input.GetLength(1) - 1];
            for(int i=0;i<column.Length;i++ )
            {
                string type = "System.String";
                string name = "thông tin " + (i + 1);
                column[i] = new DataColumn(name,typeof(string));
                dt.Columns.Add(column[i]);
                
            }
            
            for (int i = 0; i < input.GetLength(0); i++)
            {
                DataRow row = dt.NewRow();
                row["Mã khách hàng"] = input[i, 0];


                for (int j = 1; j <= column.Length; j++)
                {

                    row["thông tin " + j ] = input[i, j];
                }
                dt.Rows.Add(row);
            }
            
                return dt;
            
            
        }
    }
}
